package com.xin.springclouduser.controller;

import com.xin.springclouduser.dto.UserDTO;
import com.xin.springclouduser.enums.UserEnum;
import com.xin.springclouduser.form.UserForm;
import com.xin.springclouduser.service.UserService;
import com.xin.springclouduser.utils.JsonUtil;
import com.xin.springclouduser.utils.ResultVOUtils;
import com.xin.springclouduser.vo.ResultPage;
import com.xin.springclouduser.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 创建用户
     *
     * @param userForm
     * @return
     */
    @PostMapping("/create")
    public ResultVO create(@RequestBody(required = false) UserForm userForm) {
        if (ObjectUtils.isEmpty(userForm)) {
            return ResultVOUtils.error(1, UserEnum.FORM_PARAM_ERROR.getMessage());
        }
        UserDTO userDTO = userService.create(userForm);
        if (ObjectUtils.isEmpty(userDTO)) {
            return ResultVOUtils.error(1, UserEnum.CREATE_USER_ERROR.getMessage());
        }
        return ResultVOUtils.success();
    }

    /**
     * 修改用户信息
     *
     * @param userForm
     * @return
     */
    @PostMapping("/update")
    public ResultVO update(@RequestBody(required = false) UserForm userForm) {
        if (ObjectUtils.isEmpty(userForm)) {
            return ResultVOUtils.error(1, UserEnum.FORM_PARAM_ERROR.getMessage());
        }
        UserDTO userDTO = userService.update(userForm);
        if (ObjectUtils.isEmpty(userDTO)) {
            return ResultVOUtils.error(1, UserEnum.UPDATE_USER_ERROR.getMessage());
        }
        return ResultVOUtils.success();
    }

    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public ResultVO delete(@RequestParam(name = "id", defaultValue = "") String id) {
        boolean b = userService.delete(id);
        if (!b) {
            return ResultVOUtils.error(1, UserEnum.DELETE_USER_ERROR.getMessage());
        }
        return ResultVOUtils.success();
    }

    /**
     * 查询用户（id）
     *
     * @param id
     * @return
     */
    @GetMapping("/findOne")
    public ResultVO findOne(@RequestParam(name = "id", defaultValue = "") String id) {

        UserDTO userDTO = userService.findOne(id);
        if (ObjectUtils.isEmpty(userDTO)) {
            return ResultVOUtils.error(1, UserEnum.USER_NOT_EXIST.getMessage());
        }
        return ResultVOUtils.success(userDTO);
    }

    /**
     *模糊查询（nickname）
     * @param nickname
     * @param pageable
     * @return
     */
    @GetMapping("/findAllByLikeName")
    public ResultPage<UserDTO> findAllByLikeName(@RequestParam(value = "nickname", defaultValue = "") String nickname,
                                                 @PageableDefault(page = 1, size = 10) Pageable pageable) {
        if (StringUtils.isEmpty(nickname)){
               return null;
        }
        PageRequest pageRequest = new PageRequest(pageable.getPageNumber() - 1, pageable.getPageSize());
        ResultPage<UserDTO> dtoResultPage = userService.findAllByLikeName(nickname, pageRequest);
        return dtoResultPage;
    }

    /**
     * 查询用户(手机号)
     * @param phone
     * @return
     */
    @GetMapping("/findOneByPhone")
    public ResultVO findOneByPhone(@RequestParam(name = "phone",defaultValue = "")String phone){
        UserDTO userDTO = userService.findOneByPhone(phone);
        if (ObjectUtils.isEmpty(userDTO)){
               return ResultVOUtils.error(1,UserEnum.USER_NOT_EXIST.getMessage());
        }
        return ResultVOUtils.success(userDTO);
    }

    /**
     * 登录(正常登录)
     * @param phone
     * @param password
     * @return
     */
    @PostMapping("/login")
    public ResultVO login(@RequestParam(name = "phone",defaultValue = "")String phone,
                          @RequestParam(name = "password",defaultValue = "")String password){

        UserDTO userDTO = userService.login(phone, password);
        if (ObjectUtils.isEmpty(userDTO)){
               return ResultVOUtils.error(1,UserEnum.USER_PASSWORD_ERROR.getMessage());
        }
        return ResultVOUtils.success(userDTO);
    }
}
